1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package java.util.concurrent.atomic;
18
19
20
21
22
23
24
25 public class AtomicInteger extends Number implements java.io.Serializable {
26
27 private int value;
28
29 public AtomicInteger(int initialValue) {
30 value = initialValue;
31 }
32
33 public AtomicInteger() {
34 }
35
36 public final int get() {
37 return value;
38 }
39
40 public final void set(int newValue) {
41 value = newValue;
42 }
43
44 public final void lazySet(int newValue) {
45 set(newValue);
46 }
47
48 public final int getAndSet(int newValue) {
49 int current = value;
50 value = newValue;
51 return current;
52 }
53
54 public final boolean compareAndSet(int expect, int update) {
55 if (value == expect) {
56 value = update;
57 return true;
58 } else {
59 return false;
60 }
61 }
62
63 public final int getAndIncrement() {
64 return value++;
65 }
66
67 public final int getAndDecrement() {
68 return value--;
69 }
70
71 public final int getAndAdd(int delta) {
72 int current = value;
73 value += delta;
74 return current;
75 }
76
77 public final int incrementAndGet() {
78 return ++value;
79 }
80
81 public final int decrementAndGet() {
82 return --value;
83 }
84
85 public final int addAndGet(int delta) {
86 value += delta;
87 return value;
88 }
89
90 @Override public String toString() {
91 return Integer.toString(value);
92 }
93
94 public int intValue() {
95 return value;
96 }
97
98 public long longValue() {
99 return (long) value;
100 }
101
102 public float floatValue() {
103 return (float) value;
104 }
105
106 public double doubleValue() {
107 return (double) value;
108 }
109 }